package com.fiap.sqlite;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DatabaseHelper extends SQLiteOpenHelper {

	private static final String DATABASE_NAME = "Runners";
	private static int VERSION = 1;

	public DatabaseHelper(Context context) {
		super(context, DATABASE_NAME, null, VERSION);
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		createUserTable(db);
		createRunTable(db);
//		createUserPreferencesTable(db);
//		createFriendsTable(db);
//		createFriendsRunTable(db);
//		createInviteFriendsRunTable(db);
	}
	
	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		resetTables();
	}
	
	 /**
     * Re-create database
     * Delete all tables and create them again
     * */
    public void resetTables(){
        SQLiteDatabase db = this.getWritableDatabase();
        // Delete All Rows
        db.delete("runners_user", null, null);
        db.close();
    }
    
   
    
    /**
     * Main table of the application. Store the user information
     */
    public void createUserTable(SQLiteDatabase db){
    	db.execSQL("create table runners_user (" +
				"user_id integer primary key, " +
				"user_name text, " +
				"user_email text," +
				"user_weight text," +
				"user_height text);");
    }
    
    /**
     * 
     * Table create to store all the runs that the users ran. 
     */
    public void createRunTable(SQLiteDatabase db){
    	db.execSQL("create table runners_user_routes (" +
    			"run_id integer primary key, " +
				"user_id integer, " +
				"run_starttime datetime, " +
				"run_endtime email text," +
				"run_calburned real," +
				"run_avgspeed real," +
				"run_maxspeed real," +
				"run_distance real,"+ 
				"foreign key (user_id) references runners_user(user_id));");    	
    }
    
    /**
     * Table created to store user preferences, such as facebook accounts,
     * metrics system (kilometers & miles) etc. 
     */
    public void createUserPreferencesTable(SQLiteDatabase db){
    	db.execSQL("create table runners_user_preferences  (" +
				"user_id integer primary key, " +
				"sound_on_off integer, " +
				"language_pref text;");
    }
    
    /**
     * Create table for the user's friends.
     */
    public void createFriendsTable(SQLiteDatabase db){
    	db.execSQL("create table runners_friends  (" +
				"user_id integer primary key, " +
				"friend_id integer primary key, " +
				"friend_name text);");
    }
    
    /**
     * @param db Object responsible for executing the database commands.
     * Table created to store the friends running in the same run.
     */
    public void createFriendsRunTable(SQLiteDatabase db){
    	db.execSQL("create table runners_friends_run  (" +
				"user_id integer primary key, " +
				"friend_id integer primary key, " +
				"run_id integer primary key);");
    }
    
    /**
     * @param db Object responsible for executing the database commands.
     * Create a table to store the friends who were invited for a specific run.
     */
    public void createInviteFriendsRunTable(SQLiteDatabase db){
    	db.execSQL("create table runners_invete_friends (" +
				"user_id integer primary key, " +
				"friend_id integer primary key, " +
				"invite_time datetime );");
    }    
}
